package com.fuershiye.woman.monitor.controller;

import java.util.List;
import java.util.Map;

import com.fuershiye.woman.monitor.beans.*;
import com.fuershiye.woman.monitor.service.*;
import com.fuershiye.woman.monitor.utils.MyUtilString;
import com.fuershiye.woman.monitor.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuershiye.woman.monitor.constants.Massage;
import com.fuershiye.woman.monitor.model.FormsDepart;
import com.fuershiye.woman.monitor.model.Indicator;
import com.fuershiye.woman.monitor.utils.DateFormatUtil;
import com.google.common.collect.Lists;

/**
 * 按年份进行报表的查询
 *
 * @author yongyongdong
 *
 */
@Controller
@RequestMapping(value = "/report/analysis")
public class ReportAnalysisController {

	@Autowired
	ReportService reportService;
	@Autowired
	IndicatorService indicatorService;
	@Autowired
	DepartmentService departmentService;
	@Autowired
	FormsDepartService formsDepartService;
	@Autowired
	FormsService formsService;


	@RequestMapping(value = "/analysisView.do")
	public String preAlarm(String show, Model model) {

		List<Integer> years = DateFormatUtil.getAreaYears();
		model.addAttribute("years", years);

		List<IdName> formes = formsService.getAllNames();
		// 表 forms 的ID ： 1:妇儿 2:社会
		model.addAttribute("formes", formes);

		return "analysis/" + show;
	}


	/**
	 * 接口文档【6.1、同部门,多年份,模糊指标】
	 * 	http://bug.fuershiye.com/doc-view-25.html
	 *
	 * 同部门,多年份,模糊指标
	 *
	 * @param departid	【必须，-1:全部，只有省级部门的】
	 * @param iname	【非必须】
	 * @param years 【必传，用英文逗号隔开的年份  2011,2012,2013】
	 * @param page	【非必须】
	 * @param pageSize【非必须】
	 *depart.do
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/depart.do")
	@ResponseBody
	public Result depart(String departid, Long formsid, String iname, String years, Integer page, Integer pageSize) {

		if (StringUtils.isBlank(years) || departid == null || StringUtils.isEmpty(departid)	|| formsid == null || formsid==0) {
			return new Result(Massage.ERROR_PARAMETER);
		}

		List<Integer> yearList = DateFormatUtil.getYearStr2List(years);
		if (yearList.isEmpty()) {
			return new Result(Massage.ERROR_YEAR);
		}

		List<Long> departids;
		if(departid.equals("-1")){
			departids = null;
		}else{
			departids = MyUtilString.str2LongByPlaceHolder(departid,",");
			if (departids.isEmpty()) {
				return new Result(Massage.DEPART_IS_NULL);
			}
		}
		// 妇儿报表
		List<FormsDepart> formsDepartList = formsDepartService.listByCondtionStatusOpposite(departids, yearList, formsid, 0, new Page(1, Integer.MAX_VALUE));
		if(formsDepartList==null||formsDepartList.size()<1){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		//只查询存在上报关系年份的数据
		yearList = formsDepartService.getYears(formsDepartList);

		List<Long> departIds = Lists.newArrayList();
		// 全部，默认只查找所有省级的部门
		if(departid.equals("-1")){
			List<IdNameSname> departmentList = departmentService.getAllByLevelAndArea(2, null, 1);
			departIds = departmentService.getDepartIdsFromIdNameSname(departmentList);
		}else{
			departIds=departids;
		}
		if (departIds.isEmpty()) {
			return new Result(Massage.DEPART_IS_NULL);
		}

		List<Indicator> indicatorList =	indicatorService.listByCondition(iname, formsid, 1, new Page(1, Integer.MAX_VALUE));
		List<Long> indicators = indicatorService.getIndicatorId(indicatorList);
		List<ViewReportAllValue> reportList = reportService.getReportDate(departIds, formsid, indicators, yearList, 1);

		List<Map<String, Object>> result = reportService.filter(reportList, yearList, departIds);

		return new Result(Massage.SUCCESS, new PageResult(PageUtil.getPage(new Page(page, pageSize), result), result.size()));

	}

	/**
	 *
	 * @param years 【必传，用英文逗号隔开的年份  2011,2012,2013】
	 * @param level 部门级别 0:系统;1:国家级;2:省/部级;3:地级市;4:区县级;5:乡镇级;6:其他
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/indicator.do")
	@ResponseBody
	public Result indicator(Long formsid, Integer level, String indicators, Long areaid, String years, Integer page, Integer pageSize) {

		if (StringUtils.isBlank(years) || StringUtils.isBlank(indicators) || level==null || formsid == null || formsid==0) {
			return new Result(Massage.ERROR_PARAMETER);
		}

		List<Long> indicatorIds = MyUtilString.str2LongByPlaceHolder(indicators, ",");
		if (indicatorIds.isEmpty()) {
			return new Result(Massage.INDICATOR_IS_NULL);
		}

		List<Integer> yearList = DateFormatUtil.getYearStr2List(years);
		if (yearList.isEmpty()) {
			return new Result(Massage.ERROR_YEAR);
		}

		// 妇儿报表
		List<FormsDepart> formsDepartList = formsDepartService.listByCondtionStatusOpposite(null, yearList, formsid, 0, new Page(1, Integer.MAX_VALUE));
		if(formsDepartList==null||formsDepartList.size()<1){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		//只查询存在上报关系年份的数据
		yearList = formsDepartService.getYears(formsDepartList);

		// 查找所有省级的部门
		List<IdNameSname> departmentList = departmentService.getAllByLevelAndArea(level, areaid, 1);
		List<Long> departIds = departmentService.getDepartIdsFromIdNameSname(departmentList);
		if (departIds.isEmpty()) {
			return new Result(Massage.DEPART_IS_NULL);
		}

		List<ViewReportAllValue> reportList = reportService.getReportDate(departIds, formsid, indicatorIds, yearList, 1);
		List<Map<String, Object>> result = reportService.filter(reportList, yearList, departIds);

		return new Result(Massage.SUCCESS, new PageResult(PageUtil.getPage(new Page(page, pageSize), result), result.size()));
	}


	/**
	 * 接口文档【6.2、同部门,多年份,模糊指标】
	 * 	http://bug.fuershiye.com/doc-view-26.html
	 *
	 * 同指标领域,多年份,省级部门,模糊指标
	 *
	 * @param years 【必传，用英文逗号隔开的年份  2011,2012,2013】
	 * @param iname	【非必传】
	 * @param indicatorType	【必传：-1表示全部】
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/indicatorType.do")
	@ResponseBody
	public Result indicatorType(String iname, Integer level, Long areaid, Long formsid, String indicatorType, String years, Integer page, Integer pageSize) {

		if (StringUtils.isBlank(years) || StringUtils.isEmpty(indicatorType) || formsid == null || formsid==0) {
			return new Result(Massage.ERROR_PARAMETER);
		}

		List<Long> indicatorTypeIds = Lists.newArrayList();
		if(indicatorType.equals("-1")){
			indicatorTypeIds = null;
		}else{
			indicatorTypeIds = MyUtilString.str2LongByPlaceHolder(indicatorType, ",");
		}

		List<Integer> yearList = DateFormatUtil.getYearStr2List(years);
		if (yearList.isEmpty()) {
			return new Result(Massage.ERROR_YEAR);
		}

		// 妇儿报表
		List<FormsDepart> formsDepartList = formsDepartService.listByCondtionStatusOpposite(null, yearList, formsid, 0, new Page(1, Integer.MAX_VALUE));
		if(formsDepartList==null||formsDepartList.size()<1){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		//只查询存在上报关系年份的数据
		yearList = formsDepartService.getYears(formsDepartList);

		// 查找所有的指标
		List<Indicator> indicatorAll = indicatorService.listByCondition(iname, formsid, indicatorTypeIds, null, 1, new Page(1, Integer.MAX_VALUE));
		List<Long> indicatorIds = indicatorService.getIndicatorId(indicatorAll);
		if (indicatorIds.isEmpty()) {
			return new Result(Massage.INDICATOR_IS_NULL);
		}
		// 查找所有省级的部门
		List<IdNameSname> departmentList = departmentService.getAllByLevelAndArea(level, areaid, 1);
		List<Long> departIds = departmentService.getDepartIdsFromIdNameSname(departmentList);
		if (departIds.isEmpty()) {
			return new Result(Massage.DEPART_IS_NULL);
		}

		List<ViewReportAllValue> reportList = reportService.getReportDate(departIds, formsid, indicatorIds, yearList, 1);
		List<Map<String, Object>> result = reportService.filter(reportList, yearList, departIds);

		return new Result(Massage.SUCCESS, new PageResult(PageUtil.getPage(new Page(page, pageSize), result), result.size()));
	}



	/**
	 * 接口文档【6.2、同部门,多年份,模糊指标】
	 * 	http://bug.fuershiye.com/doc-view-26.html
	 *
	 * 同指标领域,多年份,省级部门,模糊指标
	 *
	 * @param years 【必传，用英文逗号隔开的年份  2011,2012,2013】
	 * @param iname	【非必传】
	 * @param indicatorType	【必传：-1表示全部】
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/province.do")
	@ResponseBody
	public Result province(String iname, Long areaid, Long formsid, Integer province, String indicatorType, String years, Integer page, Integer pageSize) {

		if (StringUtils.isBlank(years) || StringUtils.isEmpty(indicatorType) || formsid == null || formsid==0) {
			return new Result(Massage.ERROR_PARAMETER);
		}

		List<Long> indicatorTypeIds = Lists.newArrayList();
		if(indicatorType.equals("-1")){
			indicatorTypeIds = null;
		}else{
			indicatorTypeIds = MyUtilString.str2LongByPlaceHolder(indicatorType, ",");
		}

		List<Integer> yearList = DateFormatUtil.getYearStr2List(years);
		if (yearList.isEmpty()) {
			return new Result(Massage.ERROR_YEAR);
		}

		// 妇儿报表
		List<FormsDepart> formsDepartList = formsDepartService.listByCondtionStatusOpposite(null, yearList, formsid, 0, new Page(1, Integer.MAX_VALUE));
		if(formsDepartList==null||formsDepartList.size()<1){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		//只查询存在上报关系年份的数据
		yearList = formsDepartService.getYears(formsDepartList);

		// 查找所有的指标
		List<Indicator> indicatorAll = Lists.newArrayList();
		if(province!=null&&province==1){
			indicatorAll = indicatorService.listByCondition(iname, formsid, indicatorTypeIds, 1, 1, new Page(1, Integer.MAX_VALUE));
		}else if(province!=null&&province==0){
			indicatorAll = indicatorService.listByConditionOppositeProvince(iname, formsid, indicatorTypeIds, 1, 1, new Page(1, Integer.MAX_VALUE));
		}else{
			indicatorAll = indicatorService.listByCondition(iname, formsid, indicatorTypeIds, null, 1, new Page(1, Integer.MAX_VALUE));
		}

		List<Long> indicatorIds = indicatorService.getIndicatorId(indicatorAll);
		if (indicatorIds.isEmpty()) {
			return new Result(Massage.INDICATOR_IS_NULL);
		}
		// 查找所有省级的部门

		List<IdNameSname> departmentList = departmentService.getAllByLevelAndArea(2, areaid, 1);
		List<Long> departIds = departmentService.getDepartIdsFromIdNameSname(departmentList);
		if (departIds.isEmpty()) {
			return new Result(Massage.DEPART_IS_NULL);
		}

		List<ViewReportAllValue> reportList = reportService.getReportDate(departIds, formsid, indicatorIds, yearList, 1);
		List<Map<String, Object>> result = reportService.filter(reportList, yearList, departIds);

		return new Result(Massage.SUCCESS, new PageResult(PageUtil.getPage(new Page(page, pageSize), result), result.size()));
	}

	/**
	 * 接口文档【6.3、同部门,多年份,模糊指标】
	 * 	http://bug.fuershiye.com/doc-view-27.html
	 *
	 * 同地域,同领域,多年份,模糊查询
	 *
	 * @param areaid	【必传,地域】
	 * @param iname		【非必传,模糊指标名称查询】
	 * @param indicatorType	【必传：-1表示全部】
	 * @param years 【必传，用英文逗号隔开的年份  2011,2012,2013】
	 * @param page	【非必传】
	 * @param pageSize	【非必传】
	 * @return	返回值是value_color
	 *
	 */
	//	@RequiresPermissions("report:preAlarm")
	@RequestMapping(value = "/area.do")
	@ResponseBody
	public Result area(Integer level, String departid, Long areaid, String iname, Long formsid, String indicatorType, String years, Integer page, Integer pageSize) {

		if(areaid==null|| indicatorType==null||StringUtils.isEmpty(indicatorType) ||
				formsid == null || formsid==0 || StringUtils.isBlank(years)) {
			return new Result(Massage.ERROR_PARAMETER);
		}

		List<Integer> yearList = DateFormatUtil.getYearStr2List(years);
		if(yearList.isEmpty()){
			return new Result(Massage.ERROR_YEAR);
		}

		List<Long> indicatorTypeIds = Lists.newArrayList();
		if(indicatorType.equals("-1")){
			indicatorTypeIds = null;
		}else{
			indicatorTypeIds = MyUtilString.str2LongByPlaceHolder(indicatorType, ",");
		}

		List<FormsDepart> formsDepartList = formsDepartService.listByCondtionStatusOpposite(null, yearList, formsid, 0, new Page(1, Integer.MAX_VALUE));
		if(formsDepartList==null||formsDepartList.size()<1){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}

		// 查找所有的指标
		List<Indicator> indicatorAll = indicatorService.listByCondition(iname, formsid, indicatorTypeIds, null, 1, new Page(1, Integer.MAX_VALUE));
		List<Long> indicatorIds = indicatorService.getIndicatorId(indicatorAll);
		if (indicatorIds.isEmpty()) {
			return new Result(Massage.INDICATOR_IS_NULL);
		}

		List<Long> departids;
		if(StringUtils.isEmpty(departid) || departid.equals("-1")){
			departids = null;
		}else{
			departids = MyUtilString.str2LongByPlaceHolder(departid,",");
			if (departids.isEmpty()) {
				return new Result(Massage.DEPART_IS_NULL);
			}
		}

		Integer departLevel=null;
		if(level!=null){
			departLevel=level;
		}
		Long departAreaid=null;
		if(areaid!=null && areaid !=0 ){
			departAreaid=areaid;
		}

		List<Long> departIds = Lists.newArrayList();
		//明确查询某个部门的信息
		if(departids!=null && departids.size()>0){
			departIds=departids;
		}else{
			List<IdNameSname> departmentList = departmentService.getAllByLevelAndArea(departLevel, departAreaid, 1);
			departIds.addAll(departmentService.getDepartIdsFromIdNameSname(departmentList));
		}

		List<Map<String, Object>> result = Lists.newArrayList();
		if(departIds.size()<1){
			return new Result(Massage.SUCCESS, new PageResult(PageUtil.getPage(new Page(page, pageSize), result), result.size()));
		}
		List<ViewReportAllValue> reportList = reportService.getReportDate(departIds, formsid, indicatorIds, yearList, 1);
		result = reportService.filter(reportList, yearList, departIds);

		return new Result(Massage.SUCCESS, new PageResult(PageUtil.getPage(new Page(page, pageSize), result), result.size()));
	}

}
